<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>14章课后题3</title>
    <style>
        div {
            width: 200px;
            height: 50px;
            line-height: 50px;
            font-size: 20px;
            background-color: aliceblue;
            color: green;
            text-align: center;
            margin: 20px;
        }

        td {
            width: 70px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="show-name"></div>
    <button id="btn-start">开始</button>

    <hr />
    <table border="1">
        <tr id="tr-id"></tr>
    </table>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script>
        let userArr = ['章三', '李四', '王5', '赵六', '钱7', 'Allen', 'Bella', 'Jason', 'Joe', 'Michael'];
        let resArr = [];

        function randInt(start, end) {
            let num = Math.random();
            num = num * (end - start);
            return Math.floor(num + start);
        }

        $('#btn-start').click(function () {
            let intervalId = window.setInterval(function () {
                if (userArr.length == 0) {
                    window.clearInterval(intervalId);
                    alert('完成点名!!');
                    return;
                }
                let index = randInt(0, userArr.length);
                resArr.push(userArr[index]);
                $('#show-name').html(userArr[index]);
                userArr = userArr.slice(0, index).concat(userArr.slice(index + 1));

                let doc = "";
                for (let name of resArr) {
                    doc += `<td>${name}</td>`;
                }

                $('#tr-id').html(doc);
            }, 1000);
        });

    </script>
</body>

</html>